package priv.pront.code.robot.roboCom_na_2021;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-07-14 17:00
 */
public class 小偷踩点 {

    static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    static String[] str;
    static int n, m;
    static Map<Integer, Integer> map = new HashMap<>();

    static int[][] a;

    public static void main(String[] args) throws IOException {
        String[] s2 = bf.readLine().split(" ");
        n = Integer.parseInt(s2[0]);
        m = Integer.parseInt(s2[1]);
        str = new String[n];
        a = new int[m][10];
        for (int i = 0; i < n; i++) {
            str[i] = bf.readLine();
        }

        String[] s = bf.readLine().split(" ");
        for (int i = 0; i < m; i++) {
            map.put(Integer.parseInt(s[i]), i);
        }

        for (int i = 0; i < m; i++) {
            String[] s1 = bf.readLine().split(" ");
            for (int j = 0; j < 10; j++) {
                a[i][j] = Integer.parseInt(s1[j]);
            }
        }

        String[] s1 = bf.readLine().split(" ");
        for (int i = 1; i < s1.length; i++) {
            int num = Integer.parseInt(s1[i]);
            int row = num / 10;
            int col = num % 10;
            if(!map.containsKey(row)){
                System.out.println("?");
                continue;
            }
            int ans = a[map.get(row)][col];
            if (ans == -1) {
                System.out.println("?");
            } else {
                System.out.println(str[ans - 1]);
            }
        }


    }
}
